package com.gitee.zycra.union.api.web.controller;

import com.gitee.zycra.union.api.common.bo.CommonResult;
import com.gitee.zycra.union.api.common.enums.UserAuthEnum;
import com.gitee.zycra.union.api.common.vo.UserInfoVO;
import com.gitee.zycra.union.api.service.ApiUserGroupService;
import com.gitee.zycra.union.api.web.annotation.AuthCheck;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zycra
 */
@RestController
@RequestMapping("/{groupId}/userGroup")
public class ApiUserGroupController {

    @Autowired
    private ApiUserGroupService apiUserGroupService;

    /**
     * 添加用户到项目组
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @param auth    权限
     * @return 添加结果
     */
    @AuthCheck({"3"})
    @PostMapping("/addUser")
    public CommonResult<String> addUser(@PathVariable("groupId") Long groupId, @RequestParam("userId") Long userId,
                                        @RequestParam("auth") Integer auth) {
        if (UserAuthEnum.getByCode(auth) == null) {
            return CommonResult.failed("权限不合法");
        }
        return apiUserGroupService.addUser(groupId, userId, auth);
    }

    /**
     * 修改用户权限
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @param auth    权限
     * @return 修改结果
     */
    @AuthCheck({"3"})
    @PostMapping("/changeUserAuth")
    public CommonResult<String> changeUserAuth(@PathVariable("groupId") Long groupId, @RequestParam("userId") Long userId,
                                               @RequestParam("auth") Integer auth) {
        if (UserAuthEnum.getByCode(auth) == null) {
            return CommonResult.failed("权限不合法");
        }
        return apiUserGroupService.changeUserAuth(groupId, userId, auth);
    }

    /**
     * 移除项目组内用户
     *
     * @param groupId 项目组ID
     * @param userId  用户ID
     * @return 修改结果
     */
    @AuthCheck({"3"})
    @PostMapping("/removeUser")
    public CommonResult<String> removeUser(@PathVariable("groupId") Long groupId, @RequestParam("userId") Long userId) {
        return apiUserGroupService.removeUser(groupId, userId);
    }

    /**
     * 查询可选用户
     *
     * @param groupId 项目组ID
     * @return 查询结果
     */
    @AuthCheck({"3"})
    @GetMapping("/userList")
    public CommonResult<List<UserInfoVO>> userList(@PathVariable("groupId") Long groupId) {
        return apiUserGroupService.selectUserInfoForAddGroup(groupId);
    }
}
